<?php

namespace App\Api\Controllers\Dong_Guan;


use App\Common\PaySuccessAction;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class NotifyController extends BaseController
{

    //支付回调
    public function pay_notify_url(Request $request)
    {
        $xml = file_get_contents('php://input', 'r');
        try {
            $result = $this->xml_decode($xml);
            if ($result['status'] > 0) {
                $this->_exception($result['message']);
            }
            if (!$this->verification($result)) {
                $this->_exception("验签失败");
            }
            if ($result['result_code'] > 0) {
                $this->_exception($result['err_msg']);
            }
            $out_trade_no = $result['out_trade_no'];
            $order = Order::where('out_trade_no', $out_trade_no)->first();
            if (!$order) {
                $this->_exception('订单不存');
            }
            if ($order->pay_status == '2') {
                $baseObj = new \App\Api\Controllers\BaseController();
                //支付结果：0—成功；其它—失败
                if ($result['pay_result'] == 0) {
                    $baseObj->update_day_order([
                        'status' => 1,
                        'pay_status' => 1,
                        'pay_status_desc' => '支付成功',
                        'trade_no' => $result['out_transaction_id'],
                        'buyer_pay_amount' => bcdiv($result['total_fee'], 100, 2),
                    ], $out_trade_no);
                    //支付成功后的动作
                    $sucData = [
                        'ways_type' => $order->ways_type,
                        'company' => $order->company,
                        'ways_type_desc' => $order->ways_type_desc,
                        'source_type' => '5000', //返佣来源
                        'source_desc' => '九江银行', //返佣来源说明
                        'total_amount' => $order->total_amount,
                        'out_trade_no' => $order->out_trade_no,
                        'other_no' => $order->other_no,
                        'rate' => $order->rate,
                        'fee_amount' => $order->fee_amount,
                        'merchant_id' => $order->merchant_id,
                        'store_id' => $order->store_id,
                        'user_id' => $order->user_id,
                        'config_id' => $order->config_id,
                        'store_name' => $order->store_name,
                        'ways_source' => $order->ways_source,
                        'pay_time' => date('Y-m-d H:i:s', strtotime($result['time_end'])),
                        'device_id' => $order->device_id
                    ];
                    PaySuccessAction::action($sucData);
                } else {
                    $baseObj->update_day_order([
                        'status' => 3,
                        'pay_status' => 3,
                        'pay_status_desc' => '支付失败',
                    ], $out_trade_no);
                }
            }
        } catch (\Exception $e) {
            $msg = sprintf("== 东莞银行支付回调异常" . $e->getMessage() . " ==\n【回调内容】%s\n", $xml);
            Log::channel('daily')->debug($msg);
            $this->send($msg);
        }
        return 'success';
    }


}